package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.WaybilDetailActivity;
import com.chuanyang.driverh.response2.ResultCarPlan;
import com.chuanyang.driverh.responseresult.LookWaybillData;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.List;


public class WaybilLocationAdapter extends BaseListAdapter<LookWaybillData.DataBean> {//首页竞价大厅的adapter
    private Activity activity;
    private int bg_flag=-1;
    public WaybilLocationAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity=activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder=null;

        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_waybil_location, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_driver_car_pop = (ImageView) convertView.findViewById(R.id.iv_driver_car_pop);
            viewHolder.rb_driver_car_number = (TextView) convertView.findViewById(R.id.rb_driver_car_number);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_origin_acture = (TextView) convertView.findViewById(R.id.tv_origin_acture);
            viewHolder.tv_order_weight = (TextView) convertView.findViewById(R.id.tv_order_weight);
            viewHolder.tv_order_status = (TextView) convertView.findViewById(R.id.tv_order_status);

            viewHolder.ll_div= (LinearLayout) convertView.findViewById(R.id.ll_div);
            viewHolder.ll_right= (LinearLayout) convertView.findViewById(R.id.ll_right);
            viewHolder.ll_bg= (LinearLayout) convertView.findViewById(R.id.ll_bg);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
       final LookWaybillData.DataBean  data= getItem(position);

        if (bg_flag==position) {
            viewHolder.ll_bg.setBackgroundResource(R.color.bg_f1);
        }else {
            viewHolder.ll_bg.setBackgroundResource(R.color.white);
        }

        if (data.getFleetType()!=null && "2".equals(data.getFleetType())){
            viewHolder.iv_driver_car_pop.setImageResource(R.mipmap.car_transport);
        }else {
            viewHolder.iv_driver_car_pop.setImageResource(R.mipmap.ship_translate);
        }

//        BitmapUtil.showImgForNetPath(data.getUserImage(), viewHolder.iv_driver_car_pop);
        viewHolder.rb_driver_car_number.setText(data.getPlateNumber());
        viewHolder.tv_order_no.setText(data.getWaybillNo());
        viewHolder.tv_goods_name.setText(data.getGoodsName());
        if (data.getWaybillStatus()>3){//运输中
            //实发重量跟下单重量对应哪个字段？

            viewHolder.tv_origin_acture.setText("实发:");
            viewHolder.tv_order_weight.setText(StringUtil.getDouble(data.getWeight()+""));
        }else {
            viewHolder.tv_order_status.setText("未运");
            viewHolder.tv_origin_acture.setText("下单:");
            viewHolder.tv_order_weight.setText(StringUtil.getDouble(data.getWeight()+""));
        }

        if (data.getWaybillStatus()<=3){
            viewHolder.tv_order_status.setText("待装货");
        }else if(data.getWaybillStatus()==4){
            viewHolder.tv_order_status.setText("运输中");
        }else if(data.getWaybillStatus()==5){
            viewHolder.tv_order_status.setText("卸货中");
        }else if(data.getWaybillStatus()==6){
            viewHolder.tv_order_status.setText("待确认收货");
        }else if(data.getWaybillStatus()==7){
            viewHolder.tv_order_status.setText("订单完成");
        }else {
            viewHolder.tv_order_status.setText("其他状态");
        }









        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null)listener.onCustomerListener(v,position);
                bg_flag=position;
                notifyDataSetChanged();
            }
        });

        viewHolder.ll_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=   new Intent(activity, WaybilDetailActivity.class);
                intent.putExtra("waybilId",data.getWaybillId()+"");
                intent.putExtra("type",4);
                activity.startActivity(intent);
            }
        });

        return convertView;
    }

    private void setGoodName(TextView view, List<ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity.GoodssEntity> goodss) {
        StringBuilder	builder = new StringBuilder();
        for (ResultCarPlan.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity.GoodssEntity info: goodss){
            builder.append(info.getGoodsName() + ",");
        }
        String temp=builder.toString();
        String result=temp.substring(0,temp.length()-1);
        view.setText(result);
    }

    class ViewHolder {

        ImageView iv_driver_car_pop;
        TextView rb_driver_car_number;
        TextView tv_order_no;
        TextView tv_goods_name;
        TextView tv_origin_acture;
        TextView tv_order_weight;
        TextView tv_order_status;
        LinearLayout ll_div;
        LinearLayout ll_right;
        LinearLayout ll_bg;

    }
}
